package com.freeman.cement.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.freeman.cement.entity.EventTypeDutyRoom;
import com.freeman.cement.entity.ResponseResult;
import com.freeman.cement.exception.AppException;
import com.freeman.cement.exception.AppExceptionCodeMsg;
import com.freeman.cement.mapper.CameraDao;
import com.freeman.cement.mapper.DeptDao;
import com.freeman.cement.mapper.EventTypeDao;
import com.freeman.cement.entity.EventType;
import com.freeman.cement.mapper.EventTypeDutyRoomDao;
import com.freeman.cement.service.DutyRoomService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class DutyRoomServiceImpl implements DutyRoomService {
    //引用类型的自动注入@Autowired,@Resource
    @Resource
    private DeptDao deptDao;

    @Resource
    private EventTypeDao eventTypeDao;

    @Resource
    private CameraDao cameraDao;

    @Resource
    private EventTypeDutyRoomDao eventTypeDutyRoomDao;

    //确认值班室是否订阅了这个事件
    @Override
    public boolean Check(int dutyRoomId, String eventType) {
        boolean flag = false;
        List<EventType> list = eventTypeDao.selectSubEventType(dutyRoomId);
        for (EventType e : list) {
            if (e.getEventType().equals(eventType)) {
                flag = true;
                break;
            }
        }
        return flag;
    }

    @Override
    public ResponseResult getCameraCount(int dutyRoomId) {
        if(dutyRoomId > 0){
            return ResponseResult.success(cameraDao.getCameraCountInDutyRoom(dutyRoomId));
        } else {
            throw new AppException(AppExceptionCodeMsg.ID_IS_NULL);
        }
    }

    @Override
    public ResponseResult addSubEventType(int dutyRoomId, String eventType) {
        if(dutyRoomId <= 0 || StringUtils.isBlank(eventType)){
            throw new AppException(AppExceptionCodeMsg.CONTENT_IS_NULL);
        }
        //检查dutyRoomId是否存在 - 使用selectById替代selectDutyRoomByid
        if (deptDao.selectById(dutyRoomId) != null) {
            //检查eventType是否存在
            EventType _event = new EventType();
            _event.setEventType(eventType);
            if (eventTypeDao.selectEventTypeByNameOrType(_event).size() > 0) {
                //判断是否已经添加过此事件类型
                if (!Check(dutyRoomId, eventType)) {
                    eventTypeDao.subscribeEventType(dutyRoomId, eventType);
                    //检查是否添加成功
                    if (Check(dutyRoomId, eventType)) {
                        return ResponseResult.success("添加成功", null);
                    } else {
                        return ResponseResult.error(20002, "添加失败");
                    }
                } else {
                    return ResponseResult.error(20001, "此事件类型码已添加");
                }

            } else {
                return ResponseResult.error(20000, "事件类型码不存在");
            }
        } else {
            throw new AppException(AppExceptionCodeMsg.ID_NOT_EXISTS);
        }
    }

    @Override
    public ResponseResult deleteSubEventType(int dutyRoomId, String eventType) {
        if(dutyRoomId <= 0 || StringUtils.isBlank(eventType)){
            throw new AppException(AppExceptionCodeMsg.CONTENT_IS_NULL);
        }
        //检查dutyRoomId是否存在 - 使用selectById替代selectDutyRoomByid
        if (deptDao.selectById(dutyRoomId) != null) {
            QueryWrapper<EventTypeDutyRoom> qw = new QueryWrapper<>();
            qw.eq("dutyRoomId", dutyRoomId)
              .eq("eventType", eventType);
            
            if(eventTypeDutyRoomDao.selectCount(qw) > 0) {
                eventTypeDao.deleteSubEventType(dutyRoomId, eventType);
                if (!Check(dutyRoomId, eventType)) {
                    return ResponseResult.success("删除成功", null);
                } else {
                    return ResponseResult.error(20003, "删除失败");
                }
            } else {
                return ResponseResult.error(20004, "值班室并没有订阅此事件");
            }
        } else {
            throw new AppException(AppExceptionCodeMsg.ID_NOT_EXISTS);
        }
    }

    @Override
    public ResponseResult deleteSubEventTypeById(int id) {
        if(id > 0){
            EventTypeDutyRoom eventTypeDutyRoom = eventTypeDutyRoomDao.selectById(id);
            if(eventTypeDutyRoom != null){
                if(eventTypeDutyRoomDao.deleteById(id) > 0){
                    return ResponseResult.success("删除成功", null);
                } else {
                    return ResponseResult.error(20003, "删除失败");
                }
            } else {
                throw new AppException(AppExceptionCodeMsg.ID_NOT_EXISTS);
            }
        } else {
            throw new AppException(AppExceptionCodeMsg.ID_IS_NULL);
        }
    }

}
